const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // web项目地址
    url: '',
    // web项目携带参数
    params: '',
    // 延时返回数组
    timerList: [],
    // 第二延时返回数组
    secondTimerList: [],
    // 最后长度
    lastFlagLen: 0,
    // socket实例
    socket: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options:', options);
    // 设置web-view的地址
    this.setData({
      url: options.url || app.webViewUrl
    });
    // this.createSocket();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 换肤 白色状态栏是黑色字 其他状态栏是白色字
    if (app.statusBarInfo && app.statusBarInfo.statusBarFlag == 'Y') {
      wx.setNavigationBarColor({
        frontColor: app.statusBarInfo.statusBarColor == '#ffffff' ? '#000000' : '#ffffff',
        backgroundColor: app.statusBarInfo.statusBarColor,
        success(res) {
          console.log(res)
        },
        fail: (err) => {
          console.error(err);
        }
      })
      app.statusBarInfo.statusBarFlag = 'N'
    }
    // 修改web页面参数
    let that = this
    let resultList = JSON.parse(JSON.stringify(app.resultList));
    console.log(resultList);
    if (resultList.length == 0) {
      that.data.lastFlagLen++;
      that.setData({
        params: `?returnInfo=${encodeURIComponent(JSON.stringify({'lastFlag':'Y','lastFlagLen':that.data.lastFlagLen}))}`
      })
    } else {
      that.data.lastFlagLen = 0;
      resultList.forEach(function (item, i) {
        that.data.timerList.push('timer' + i);
        that.data.timerList[i] = setTimeout(function () {
          if (i == resultList.length - 1) {
            item['lastFlag'] = 'Y'
          }
          let dataArr = item.message.data;
          if (Array.isArray(dataArr)) {
            let currentItem = JSON.parse(JSON.stringify(item));
            currentItem.message.data = {};
            currentItem.isPicture = "Y";
            currentItem.pictureLen = dataArr.length;
            that.setData({
              params: `?returnInfo=${encodeURIComponent(JSON.stringify(currentItem))}`
            })
            dataArr.forEach(function (pro, n) {
              that.data.secondTimerList[n] = setTimeout(function () {
                that.setData({
                  params: `?returnInfo=${encodeURIComponent(JSON.stringify(pro))}`
                })
                clearTimeout(that.data.secondTimerList[n])
              }, 1000 * (n + 1))
            })
            app.resultList.shift();
            clearTimeout(that.data.timerList[i])
          } else {
            that.setData({
              params: `?returnInfo=${encodeURIComponent(JSON.stringify(item))}`
            })
            app.resultList.shift();
            clearTimeout(that.data.timerList[i])
          }
        }, 1000 * i)
        if (i == resultList.length - 1) {
          that.data.timerList = [];
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log('页面隐藏');
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (!this.data.socket) return;
    this.data.socket.close();
  },

  /**
   * 创建socket
   */
  createSocket() {
    let _this = this;
    let socket = wx.connectSocket({
      url: 'ws://fzhaitaiinc.com:56703/websocket',
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        wx.onSocketOpen(()=>{
          console.log('客户端连接成功');
          _this.setData({
            socket: socket
          });
          // 发送请求表明身份
          wx.sendSocketMessage({
            data: JSON.stringify(_this.data.msg)
          });
          // 监听socket
          wx.onSocketMessage((res) => {
            let messageInfo = JSON.parse(res.data);
            // console.log('接收到的信息', messageInfo);
            _this.selectionMethod(messageInfo);
          })
        })
      }
    })
  },

  /**
   * 根据消息对象调用对应方法
   * @param { Object } messageInfo 消息对象
   */
  selectionMethod(messageInfo) {
    console.log('获取消息值',messageInfo);
  }
})